import {Service} from "typedi";
import User from "../models/user/user";

const jwt = require('jsonwebtoken')
const crypto = require('crypto')
import UserAuth from "../models/user/user-auth";

@Service()
export class UserService {

  findAll() {
    return User.findAll()
  }

  findOne(id: number) {
    return User.findOne({where: {id}})
  }

  async create(user: User) {
    let checkUser = await User.findOne({where: {username: user.username}})
    console.log('checkUser')
    console.log(checkUser !== null)
    if (checkUser) {
      return '注册失败！该用户名已被使用。'
    }
    let result = await user.save()
    console.log(result, 'result')
    return result.toJSON()
  }

  update(user: User) {
    return User.update(user, {where: {id: user.id}})
  }

  remove(id: number) {
    return User.destroy({where: {id}})
  }

}
